<template>
    <div class="w">
      <el-breadcrumb separator="/">
        <el-breadcrumb-item :to="{ path:'/manage' }">首页</el-breadcrumb-item>
        <el-breadcrumb-item>权限管理</el-breadcrumb-item>
        <el-breadcrumb-item>所有权限</el-breadcrumb-item>
      </el-breadcrumb>
      <div class="add">
        <el-button type="info" icon="el-icon-circle-plus-outline" size="mini" plain @click="addPrivilege">添加权限</el-button>
      </div>
      <el-table :data="priData" stripe size="small">
        <el-table-column type="index" width="70"/>
        <el-table-column prop="name" label="权限名"/>
        <el-table-column prop="uri" label="权限uri"/>
        <el-table-column prop="description" label="权限描述"/>
        <el-table-column label="权限等级">
          <template slot-scope="scope">
            <el-tag :type="scope.row.isSensitive === 1 ? 'danger' : ''">{{ scope.row.isSensitive === 1 ? '敏感权限' : (scope.row.isSensitive === 2 ? '公共权限' : '普通权限')}}</el-tag>
          </template>
        </el-table-column>
        <el-table-column label="操作">
          <template slot-scope="scope">
            <el-button type="primary" size="mini" plain @click="update(scope.$index, scope.row)">修改</el-button>
            <el-button v-if="scope.row.status===1" type="danger" size="mini" plain @click="btnDisable(scope.row)">禁用</el-button>
            <el-button v-if="scope.row.status===0" type="success" size="mini" plain @click="btnEnable(scope.row)">启用</el-button>
          </template>
        </el-table-column>
      </el-table>
      <div class="page">
        <el-pagination
          @size-change="pageSizeChange"
          @current-change="pageChange"
          :background="true"
          :current-page="1"
          :page-sizes="[10, 15, 20, 25]"
          :page-size="page.pageSize"
          layout="total, sizes, prev, pager, next, jumper"
          :total="page.pageTotal">
        </el-pagination>
      </div>
      <el-dialog title="权限修改" :visible.sync="dialogFormVisible" width="40%">
        <el-form :model="form">
          <el-form-item label="权限名" :label-width="formLabelWidth">
            <el-input v-model="form.name" auto-complete="on"/>
          </el-form-item>
          <el-form-item label="权限uri" :label-width="formLabelWidth">
            <el-input v-model="form.uri" auto-complete="on"/>
            <el-button type="text" size="mini" @click="selectPri">选择权限</el-button>
          </el-form-item>
          <el-form-item label="权限描述" :label-width="formLabelWidth">
            <el-input v-model="form.description" auto-complete="on"/>
          </el-form-item>
          <el-form-item label="权限等级">
            <el-radio-group v-model="form.isSensitive">
              <el-radio :label="0">普通权限</el-radio>
              <el-radio :label="1">敏感权限</el-radio>
              <el-radio :label="2">公共权限</el-radio>
            </el-radio-group>
          </el-form-item>
        </el-form>
        <div slot="footer" class="dialog-footer">
          <el-button @click="dialogFormVisible = false" size="mini">取 消</el-button>
          <el-button @click="updatePermission" type="primary" :loading="state" size="mini">确 定</el-button>
        </div>
        <el-dialog
          width="80%"
          title="请选择"
          :visible.sync="innerUpdateVisible"
          append-to-body>
          <el-table
            @cell-click="clickUpdateCell"
            :data="innerTabData"
            :border="true"
            size="mini"
            style="width: 100%;cursor: pointer;">
            <el-table-column
              prop="uri1"
              label="资源"
              width="200">
            </el-table-column>
            <el-table-column
              prop="uri2"
              width="320"
              label="资源">
            </el-table-column>
            <el-table-column
              prop="uri3"
              width="350"
              label="资源">
            </el-table-column>
            <el-table-column
              prop="uri4"
              label="资源">
            </el-table-column>
          </el-table>
        </el-dialog>
      </el-dialog>
      <el-dialog title="添加权限" :visible.sync="addPrivilegeVisible" width="40%" @close="addDialogClose">
        <el-form :model="addForm" :rules="rules" ref="ruleForm" @validate="validate">
          <el-form-item label="权限名" :label-width="formLabelWidth" prop="name">
            <el-input v-model="addForm.name" auto-complete="off"/>
          </el-form-item>
          <el-form-item label="权限uri" :label-width="formLabelWidth" prop="uri">
            <el-input v-model="addForm.uri" auto-complete="off" style="width: 80%;"/>
            <el-button type="text" size="mini" @click="selectPri">选择权限</el-button>
          </el-form-item>
          <el-form-item label="权限描述" :label-width="formLabelWidth">
            <el-input v-model="addForm.description" auto-complete="off"/>
          </el-form-item>
          <el-form-item label="权限等级">
            <el-radio-group v-model="addForm.isSensitive">
              <el-radio :label="0">普通权限</el-radio>
              <el-radio :label="1">敏感权限</el-radio>
              <el-radio :label="2">公共权限</el-radio>
            </el-radio-group>
          </el-form-item>
        </el-form>
        <div slot="footer" class="dialog-footer">
          <el-button size="mini" @click="addParams">继续添加</el-button>
          <el-button @click="addPrivilegeVisible = false" size="mini">取 消</el-button>
          <el-button type="primary" @click="send" :loading="state" size="mini" :disabled="!btnSub">确 定</el-button>
        </div>
        <div>
          <ul>
            <li v-for="param in permissions" :key="param.name">
              <span>name:{{param.name}}</span>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;
              <span>uri:{{param.uri}}</span>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;
              <span>des:{{param.description}}</span>
            </li>
          </ul>
        </div>
        <el-dialog
          width="80%"
          title="请选择"
          :visible.sync="innerVisible"
          append-to-body>
          <el-table
            @cell-click="clickCell"
            :data="innerTabData"
            :border="true"
            size="mini"
            style="width: 100%;cursor: pointer;">
            <el-table-column
              prop="uri1"
              label="资源"
              width="200">
            </el-table-column>
            <el-table-column
              prop="uri2"
              width="320"
              label="资源">
            </el-table-column>
            <el-table-column
              prop="uri3"
              width="350"
              label="资源">
            </el-table-column>
            <el-table-column
              prop="uri4"
              label="资源">
            </el-table-column>
          </el-table>
        </el-dialog>
      </el-dialog>
    </div>
</template>

<script>
export default {
  name: 'privilege',
  data () {
    return {
      priData: [],
      formLabelWidth: '70px',
      form: {},
      addForm: {
        name: '',
        description: '',
        uri: '',
        isSensitive: 0,
        status: 1
      },
      rules: {
        name: [
          {required: true, message: '请输入名称', trigger: 'blur'}
        ],
        uri: [
          {required: true, message: '请输入uri', trigger: 'blur'}
        ]
      },
      dialogFormVisible: false,
      innerVisible: false,
      innerUpdateVisible: false,
      addPrivilegeVisible: false,
      state: false,
      permissions: [],
      btnSub: false,
      page: {
        pageTotal: 0,
        pageSize: 10,
        pageNum: 1,
        nextPage: 2
      },
      oldData: {},
      innerTabData: []
    }
  },
  methods: {
    clickCell (row, column) {
      this.addForm.uri = row[column.property]
      this.innerVisible = false
    },
    clickUpdateCell (row, column) {
      this.form.uri = row[column.property]
      this.innerUpdateVisible = false
    },
    selectPri () {
      this.innerTabData = []
      if (this.dialogFormVisible) {
        this.innerUpdateVisible = true
      } else if (this.addPrivilegeVisible) {
        this.innerVisible = true
      }
      this.$ajax.get('/path/get').then(res => {
        let l = Math.ceil(res.data.data.length / 4)
        let f = res.data.data.splice(0, l)
        let s = res.data.data.splice(0, l)
        let t = res.data.data.splice(0, l)
        for (let i = 0; i < l; i++) {
          this.innerTabData.push({
            uri1: f[i],
            uri2: s[i],
            uri3: t[i],
            uri4: res.data.data[i]
          })
        }
      })
    },
    update (index, row) {
      this.form = {
        name: row.name,
        description: row.description,
        uri: row.uri,
        isSensitive: row.isSensitive
      }
      this.oldData = JSON.parse(JSON.stringify(this.form))
      this.oldData.id = row.id
      this.dialogFormVisible = true
      this.state = false
    },
    updatePermission () {
      let params = {}
      let isChange = false
      if (this.oldData.name !== this.form.name) {
        params.name = this.form.name
        isChange = true
      }
      if (this.oldData.uri !== this.form.uri) {
        params.uri = this.form.uri
        isChange = true
      }
      if (this.oldData.isSensitive !== this.form.isSensitive) {
        params.isSensitive = this.form.isSensitive
        isChange = true
      }
      if (this.oldData.description !== this.form.description) {
        params.description = this.form.description
        isChange = true
      }
      if (isChange) {
        params.id = this.oldData.id
        this.$ajax.post('/admin/manager/permission/update', JSON.stringify(params)).then((res) => {
          if (res.data.success) {
            this.loadMore(this.page.pageNum, this.page.pageSize)
            this.$message({
              type: 'success',
              message: '修改成功'
            })
            this.dialogFormVisible = false
          }
        })
      }
    },
    send () {
      if (!(this.addForm.name === '' || this.addForm.uri === '')) {
        if (this.addForm.description === '') {
          this.addForm.description = '无'
        }
        this.permissions.push({
          name: this.addForm.name,
          uri: this.addForm.uri,
          description: this.addForm.description,
          status: 1,
          isSensitive: this.addForm.isSensitive,
          permissionSetId: 0
        })
      }
      this.state = true
      this.$ajax.post('/admin/manager/permission/add', JSON.stringify(this.permissions)).then((res) => {
        this.addPrivilegeVisible = false
        console.log(res)
        if (res.data.success) {
          this.$message({
            type: 'success',
            message: '添加成功'
          })
        }
        this.loadMore(1, 5)
      }).catch((err) => {
        console.log(err)
      })
    },
    btnDisable (row) {
      this.$confirm('此操作将禁用该权限，所有拥有该权限的角色都会失效，是否继续？', '提示', {
        confirmButtonText: '确定',
        cancelButtonText: '取消',
        type: 'warning'
      }).then(() => {
        let params = []
        params.push(row.id)
        this.$ajax.post('/admin/manager/permission/disable', JSON.stringify(params)).then((res) => {
          console.log(res)
          this.$message({
            type: 'success',
            message: '禁用成功！'
          })
          this.loadMore(this.page.pageNum, this.page.pageSize)
        }).catch((err) => {
          console.log(err)
        })
      }).catch(() => {
        this.$message({
          type: 'info',
          message: '已取消禁用'
        })
      })
    },
    btnEnable (row) {
      this.$confirm('此操作将启用该权限，所有拥有该权限的角色都会生效，是否继续？', '提示', {
        confirmButtonText: '确定',
        cancelButtonText: '取消',
        type: 'warning'
      }).then(() => {
        let params = []
        params.push(row.id)
        this.$ajax.post('/admin/manager/permission/enable', JSON.stringify(params)).then((res) => {
          if (res.data.success) {
            this.$message({
              type: 'success',
              message: '启用成功！'
            })
            this.loadMore(this.page.pageNum, this.page.pageSize)
          }
        })
      }).catch(() => {
        this.$message({
          type: 'info',
          message: '已取消启用'
        })
      })
    },
    addPrivilege () {
      this.state = false
      this.addPrivilegeVisible = true
    },
    addParams () {
      if (this.addForm.description === '') {
        this.addForm.description = '无'
      }
      if (this.addForm.name === '' || this.addForm.uri === '') {
        alert('XXX')
        return
      }
      this.permissions.push({
        name: this.addForm.name,
        uri: this.addForm.uri,
        description: this.addForm.description,
        status: 1,
        isSensitive: this.addForm.isSensitive,
        permissionSetId: 0
      })
      this.addForm.description = ''
      this.$refs.ruleForm.resetFields()
      // console.log(JSON.stringify(this.test))
    },
    validate (prop, isPass) {
      this.btnSub = isPass
    },
    addDialogClose () {
      this.permissions = []
    },
    loadMore (pageNum, pageSize) {
      let params = {
        pageNum,
        pageSize
      }
      this.$ajax.post('/admin/manager/permission/list', JSON.stringify(params)).then((res) => {
        this.priData = res.data.data.list
        this.page.pageTotal = res.data.data.total
        this.page.pageSize = res.data.data.pageSize
        this.page.pageNum = res.data.data.pageNum
        this.page.nextPage = res.data.data.nextPage
      })
    },
    pageSizeChange (pageSize) {
      this.loadMore(this.page.pageNum, pageSize)
    },
    pageChange (pageNum) {
      this.loadMore(pageNum, this.page.pageSize)
    }
  },
  created () {
    this.loadMore(1, 5)
  }
}
</script>

<style scoped>
  .add{
    margin-left: 10px;
    margin-bottom: 10px;
  }
  .el-input{
    width: 450px;
  }
  .el-select{
    width: 450px;
  }
  .dialog-footer{
    margin-top: -30px;
  }
  .page{
    padding: 20px;
    text-align: center;
    background: #fff;
  }
</style>
